﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DataAccess;

namespace BusinessLayer
{
    public class FaultService : IFaultService
    {
        /// <summary>
        /// Generating an random Number for Ticket NUmber
        /// </summary>
        /// <returns></returns>
        public string GenerateNumber()
        {
            string Ticket = "";

            for (int i = 0; i < 6; i++)
            {
                Random r = new Random();
                Ticket += r.Next(0, 10).ToString();
            }

            return Ticket;
        }
        /// <summary>
        /// Login for when the user whats to report a fault on a product
        /// </summary>
        /// <param name="username"></param>
        /// <param name="Token"></param>
        /// <param name="pin"></param>
        /// <returns></returns>
        public bool FaultLogin(string username,string Token, string pin)
        {
            Encryption enc = new Encryption();
            string pass = enc.DecryptTripleDES(Token, pin);

            bool found = new UserRepository().UserLogin(username, pass);

            if (found == true)
                return true;
            else
                return false;
        }
        /// <summary>
        /// Adding a product Fault that the user reported in the database
        /// </summary>
        /// <param name="username"></param>
        /// <param name="ProductID"></param>
        /// <param name="details"></param>
        public void FileFaultReport(string username, int ProductID,string details)
        {
            new FaultRepository().AddFaultDetails(username, ProductID,details);
        }
        /// <summary>
        /// Getting A products that have a fault between a date range
        /// </summary>
        /// <param name="ProductID"></param>
        /// <param name="startdate"></param>
        /// <param name="endDate"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public IEnumerable<Common.UserProductFault> ListOfItemFaults(int ProductID, DateTime startdate, DateTime endDate, string username)
        {
            return new FaultRepository().ListOfItemFaults(ProductID, startdate, endDate, username);
        }

        /// <summary>
        /// Getting all Faults from the database
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Common.UserProductFault> getAllFaults()
        {
            return new FaultRepository().getAllFaults();
        }
        /// <summary>
        /// Getting a fault by its ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Common.UserProductFault getFaultByID(int id)
        {
            return new FaultRepository().getFaultByID(id); 
        }

        /// <summary>
        /// Getting a fault by FaultID and OrderID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public Common.UserProductFault getFaultByIDOrderID(int id, Guid orderID)
        {
            return new FaultRepository().getFaultByID(id, orderID);
        }

        /// <summary>
        /// Getting Al fault Statues Like Picked etc
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Common.Fault> GetAllFaultStatus()
        {
            return new FaultRepository().GetAllFaultStatus();
        }

        /// <summary>
        /// Getting a fault Statuse by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Common.Fault getFaultStatusByID(int id)
        {
            return new FaultRepository().getFaultStatusByID(id);
        }

        /// <summary>
        /// Add a record of a fault
        /// </summary>
        /// <param name="fault"></param>
        public void addFaultLog(Common.FaultLog fault)
        {
            new FaultRepository().addFaultLog(fault);
        }

        /// <summary>
        /// Updating a fault Details
        /// </summary>
        /// <param name="fault"></param>
        public void UpdateFault(Common.UserProductFault fault)
        {
            new FaultRepository().UpdateFault(fault);
        }

        /// <summary>
        /// Printing a Report in A PDF format of the Fault reported by the user
        /// </summary>
        /// <param name="fault"></param>
        /// <returns></returns>
        public string CreatePDFFault(Common.UserProductFault fault)
        {
            return new FaultRepository().CreatePDFFault(fault);
        }
    }
}
